<template>
  <div class="dashboard-container">
    <div class="top">
      <div class="lel">
        <el-row :gutter="20">
          <el-col class="oneBg" :span="13">
            <div class="header">
              <h3>工单统计</h3>
              <p>{{ workorder.time }}</p>
            </div>
            <div class="footer">
              <div class="text">
                <h3>{{ workorder.totall }}</h3>
                <p>工单总数(个)</p>
              </div>
              <div class="text">
                <h3>{{ workorder.accomplish }}</h3>
                <p>完成工单(个)</p>
              </div>
              <div class="text">
                <h3>{{ workorder.padding }}</h3>
                <p>进行工单(个)</p>
              </div>
              <div class="text">
                <h3>{{ workorder.cancel }}</h3>
                <p>取消工单(个)</p>
              </div>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="header">
              <h3>销售统计</h3>
              <p>{{ workorder.time }}</p>
            </div>
            <div class="footer">
              <div class="text">
                <h3>{{ mountZ }}</h3>
                <p>订单量(个)</p>
              </div>
              <div class="text">
                <h3>{{ xsE }}</h3>
                <p>销售额(万元)</p>
              </div>
            </div>
          </el-col>
        </el-row>
        <div class="echarts">
          <div class="echatrH">
            <div class="text">
              <h3>销售数据</h3>
              <p>{{ workorder.time }}</p>
            </div>
            <div class="btn">
              <el-button size="mini" @click="weekFn">周</el-button>
              <el-button size="mini" @click="mothedFn">月</el-button>
              <el-button size="mini" @click="yearFn">年</el-button>
            </div>
          </div>
          <div class="echartT">
            <div class="lel" ref="zxt">ffsdsdf</div>
            <div class="rig" ref="recharts">fsdfds</div>
          </div>
        </div>
      </div>
      <div class="righ">
        <el-row class="right">
          <h3>商品热榜</h3>
          <p>{{ workorder.time }}</p>
        </el-row>
        <div class="listShop">
          <el-row v-for="(item,index) in reList" :key="index" class="rem">
            <el-col class="zuoc" :span="2">{{ index+1 }}</el-col>
            <el-col class="zuoc" :span="14">{{ item.skuName }}</el-col>
            <el-col class="zuoc" :span="6">{{ item.count }}单</el-col>
          </el-row>
        </div>
      </div>
    </div>
    <div class="button"></div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  getGdxqApi,
  getperopeList,
  getXs,
  getRem,
  zxTj,
  zzT,
} from '@/api/dashboard'
import moment from 'moment'
import * as echarts from 'echarts'
import dayjs from 'dayjs'
export default {
  name: 'Dashboard',
  data() {
    return {
      workorder: {
        time: null,
        totall: 0,
        accomplish: 0,
        padding: 0,
        cancel: 0,
      },
      start: '',
      end: '',
      week: '',
      year: '',
      mothedStard: '',
      mothedEnd: '',
      mountZ: 0,
      xsE: 0,
      reList: [],
      zxList: [],
      newList: [],
      addList: [],
      zhuSList: [],
      zhuXList: [],
    }
  },
  computed: {
    ...mapGetters(['name']),
  },
  async created() {
    await this.$nextTick()
    this.getGdxqApi()
    this.mothedFn()
    // this.weekFn()
  },
  methods: {
    async getGdxqApi() {
      this.start = dayjs().startOf('month').format('YYYY-MM-DD hh:mm:ss')
      this.end = dayjs().format('YYYY-MM-DD hh:mm:ss')
      this.mothedStard = dayjs().startOf('month').format('YYYY-MM-DD')
      this.mothedEnd = dayjs().format('YYYY-MM-DD')
      this.workorder.time = this.mothedStard + '~' + this.mothedEnd
      this.week = dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD')
      this.year = dayjs().startOf('year').format('YYYY-MM-DD')
      // 获取工单统计
      const res = await getGdxqApi(this.start, this.end)
      this.workorder.totall = res.reduce(
        (prev, item) => (prev += item.total),
        0
      )
      this.workorder.accomplish = res.reduce(
        (prev, item) => (prev += item.completedTotal),
        0
      )
      this.workorder.padding = res.reduce(
        (prev, item) => (prev += item.progressTotal),
        0
      )
      this.workorder.cancel = res.reduce(
        (prev, item) => (prev += item.cancelTotal),
        0
      )
      // 获取总人员数
      this.mountZ = await getperopeList(this.start, this.end)
      // 获取销售额
      this.xsE = ((await getXs(this.start, this.end)) * 0.00001).toFixed(2)
      // 获取排行榜
      this.reList = await getRem(10, this.mothedStard, this.mothedEnd)
    },
    // 点击周
    async weekFn() {
      // 获取折线图
      const res2 = await zxTj(1, this.week, this.mothedEnd)
      const arr = [
        '星期日',
        '星期一',
        '星期二',
        '星期三',
        '星期四',
        '星期五',
        '星期六',
      ]
      const arr1 = res2.xAxis.map((ele) => arr[moment(ele).day()])
      const  arr2 = res2.series.map((ele) => (ele * 0.01).toFixed(2))
      this.newList = arr1
      this.addList = arr2
      this.getxiaoshou()
      // 获取柱状图
      const res = await zzT(this.week, this.mothedEnd)
      this.zhuSList = res.series.map((ele) => (ele / 100).toFixed(2))
      this.zhuXList = res.xAxis
      this.getDistribute()
    },
    // 点击月
    async mothedFn() {
      // 折线图
      const res2 = await zxTj(1, this.mothedStard, this.mothedEnd)
      const arr = res2.xAxis.map((ele, index) => {
        if (index % 3 === 0 || index % 3 === 3) {
          return ele
        }
      })
      this.newList = Array.from(new Set(arr)).filter((ele) => ele !== undefined)
      this.addList = res2.series.map((ele) => (ele * 0.01).toFixed(2))
      console.log(this.newList)
      console.log(this.addList)
      this.getxiaoshou()
      // 获取柱状图
      const res = await zzT(this.mothedStard, this.mothedEnd)
      this.zhuSList = res.series.map((ele) => (ele / 100).toFixed(2))
      this.zhuXList = res.xAxis
      this.getDistribute()
    },
    // 点击年
    async yearFn() {
      // 折线图
      const res2 = await zxTj(1, this.year, this.mothedEnd)
      const arr1 = (
        res2.series.reduce((pre, item, index) => {
          if (index <= 30) {
            pre = pre + item
          }
          return pre
        }, 0) / 100
      ).toFixed(2)
      const arr2 = (
        res2.series.reduce((pre, item, index) => {
          if (index > 30 && index <= 60) {
            pre = pre + item
          }
          return pre
        }, 0) / 100
      ).toFixed(2)
      const arr3 = (
        res2.series.reduce((pre, item, index) => {
          if (index > 60) {
            pre = pre + item
          }
          return pre
        }, 0) / 100
      ).toFixed(2)
      this.addList= [arr1,arr2,arr3]
      console.log(this.addList);
      this.newList =[ res2.xAxis[0],res2.xAxis[31],res2.xAxis[59]]
      this.getxiaoshou()
      // 柱状图
      const res = await zzT(this.year, this.mothedEnd)
      this.zhuSList = res.series.map((ele) => (ele / 100).toFixed(2))
      this.zhuXList = res.xAxis
      this.getDistribute()
    },
    // 折线图
    getxiaoshou() {
      console.log(this.newList)
      const myChart = echarts.init(this.$refs.zxt)
      const option = {
        title: {
          text: '销售额趋势图',
          left: '50%',
          textStyle: {
            fontSize: 20,
          },
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.newList,

          axisLabel: {
            color: '#ccc',
            fontSize: '14px',
          },
        },
        yAxis: {
          type: 'value',
          name: '单位：元',
          nameTextStyle: {
            color: '#999',
            fontSize: '14px',
          },
          splitLine: {
            show: true,
          },
          axisLabel: {
            color: '#ccc',
            fontSize: '14px',
          },
        },
        series: [
          {
            data: this.addList,
            type: 'line',
            areaStyle: {},
            lineStyle: {
              width: 5,
              color: '#ff8081',
            },
            smooth: true
          },
        ],
        tooltip: {
          show: true,
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              type: 'solid',
              color: '#d9d9d9',
            },
          },
        },
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: '#f6afae', // 0% 处的颜色
            },
            {
              offset: 1,
              color: '#fff', // 100% 处的颜色
            },
          ],
          global: false, // 缺省为 false
        },
      }
      myChart.setOption(option)
    },
    //柱状图
    getDistribute() {
      const myChart = echarts.init(this.$refs.recharts)
      const option = {
        title: {
          text: '销售额分布',
          left: '50%',

          textStyle: {
            fontSize: 20,
          },
        },
        xAxis: {
          type: 'category',
          data: this.zhuXList,
          axisLabel: {
            color: '#ccc',
            fontSize: '14px',
          },
        },

        yAxis: {
          type: 'value',
          axisLabel: {
            color: '#ccc',
            fontSize: '14px',
          },
          nameTextStyle: {
            color: '#999',
            fontSize: '14px',
          },
          name: '单位：元',
        },
        tooltip: {
          show: true,
        },
        series: [
          {
            data: this.zhuSList,
            type: 'bar',
            barMaxWidth: 30,
            itemStyle: {
              borderRadius: [10, 10, 0, 0],
            },
          },
        ],
      }
      myChart.setOption(option)
    },
  },
}
</script>

<style lang="scss" scoped>
.dashboard {
  background-color: #f8fafd;
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.top {
  display: flex;
  width: 1450px;
  height: 538px;
  // background-color: pink;
}
.lel {
  width: 1110px;
  // background-color: red;
}
.righ {
  flex: 1;
  margin-left: 10px;
  .right {
    height: 50px;
    display: flex;
    align-items: center;
    p {
      margin-left: 20px;
    }
  }
  .listShop {
    width: 100%;
    height: 465px;
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col:nth-child(-n + 2) {
  border-radius: 30px;
  height: 166px;
  &:nth-child(1) {
    margin-left: 10px;
    background: url()
        left top no-repeat,
      url(https://likede2-admin.itheima.net/img/task.66b715b7.png) right bottom
        repeat-y;
    background-color: #e9f3ff;
    .header {
      display: flex;
      align-items: center;
      height: 40px;
      margin-top: 8px;
      h3 {
        font-size: 16px;
      }
      p {
        margin-left: 20px;
      }
    }
    .footer {
      display: flex;
      height: 100px;
      justify-content: space-around;
      align-items: center;
      h3 {
        font-size: 35px;
        color: #072074;
      }
      p {
        color: #99acdd;
        transform: translateY(-30px);
      }
    }
  }
  &:nth-child(2) {
    margin-left: 20px;
    background: #fbefe8
      url('https://likede2-admin.itheima.net/img/sale.606b0d8c.png') right
      bottom no-repeat;
    .header {
      display: flex;
      align-items: center;
      height: 40px;
      margin-top: 8px;
      h3 {
        font-size: 16px;
      }
      p {
        margin-left: 20px;
      }
    }
    .footer {
      display: flex;
      height: 100px;
      justify-content: space-around;
      align-items: center;
      h3 {
        font-size: 35px;
        color: #072074;
      }
      p {
        color: #99acdd;
        transform: translateY(-30px);
      }
    }
  }
}
.echarts {
  // background-color: pink;
  height: 352px;
  .echatrH {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .text {
      display: flex;
      h3 {
        font-size: 18px;
      }
      p {
        margin-top: 20px;
        margin-left: 10px;
      }
    }
    .btn {
      width: 180px;
      height: 34px;
      padding-top: 3px;
      text-align: center;
      background-color: #f7fbff;
    }
  }
  .echartT {
    height: 300px;
    display: flex;
    .lel {
    width: 650px;
  }
  .rig {
    width: 500px;
    flex: 1;
  }
  }
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.rem {
  display: flex;
  height: 27px;
  line-height: 27px;
  // align-items: center;
  font-size: 17px;
  .zuoc {
    background: url('') !important;
  }
}
</style>
